import CryptoJS from 'crypto-js'
import dayjs from 'dayjs'


Page({
  data: {
    // 所有步骤
    steps: [
      {
        title: '1.扫描二维码',
        description: '',
      },
      {
        title: '2.选择到期时间',
        description: '',
      },
      {
        title: '3.生成注册码',
        description: '',
      },
    ],
    // 当前步骤下标
    currentStep: 0,
    // 选择的日期
    selectDate: null,

    // 返回/显示 码
    viewCode: null,
    // md5内容
    md5Content: "",
    // 注册码
    registrationCode: ""
  },
  onLoad(query) {
    // 页面加载
    console.info(`Page onLoad with query: ${JSON.stringify(query)}`);


  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
  // 扫码按钮点击事件
  scanClick() {
    console.log('扫码');
    dd.scan({
      type: 'qr',
      success: (res) => {
        console.log(res);
        const { text, code } = res
        // 判断内容是否为空
        if (code != null && code != undefined && code != '') {

          let codeContent = code
          // 判断内容是否携带标识
          if (codeContent.indexOf("lohand") != -1) {
            let md5Content = CryptoJS.MD5(codeContent).toString().substr(0, 3)


            this.setData({
              md5Content: md5Content,
              currentStep: 1
            })
            dd.showToast({
              content: "识别成功",
              type: "success",
              duration: 1200
            })
          } else {
            dd.alert({
              title: '提示',
              content: '二维码内容格式错误',
              buttonText: '我知道了',
              success: () => { },
              fail: () => { },
              complete: () => { },
            });
          }
        } else {
          dd.alert({
            title: '提示',
            content: '未识别到内容',
            buttonText: '我知道了',
            success: () => { },
            fail: () => { },
            complete: () => { },
          });
        }

      },
      fail: () => {
        dd.alert({
          title: '提示',
          content: '扫码失败',
          buttonText: '我知道了',
          success: () => { },
          fail: () => { },
          complete: () => { },
        });
      },
      complete: () => { },
    });
  },
  // 生成注册码按钮点击事件
  createCode() {
    // 判断是否选择时间
    if (this.data.selectDate) {
      console.log(this.data.md5Content);
      let time = Number(this.data.selectDate).toString(16)
      let viewCode = this.data.md5Content + time

      this.setData({
        currentStep: 2,
        viewCode: viewCode
      })
    } else {
      dd.alert({
        title: '提示',
        content: '请选择到期日期',
        buttonText: '我知道了',
        success: () => { },
        fail: () => { },
        complete: () => { },
      });
    }

  },
  removeChars(str, char) {
    let regex = new RegExp(char, 'g');
    return str.replace(regex, '');
  },
  // 日期选择ok点击事件
  dateSelectOkClick(date, dateStr, e) {
    let tDate = this.removeChars(dateStr, '/').slice(2)
    console.log(tDate)
    this.setData({
      selectDate: tDate
    })
  },
  // 复制点击事件
  copyClick() {
    dd.setClipboard({
      text: this.data.viewCode,
      success: (res) => {
        dd.showToast({
          content: "复制成功",
          type: "success",
          duration: 1200,
        })
      }
    });
  },
  // 重新扫码点击事件
  resetBtnClick() {
    this.setData({
      currentStep: 0,
      selectDate: null,
      viewCode: null,
      md5Content: "",
      registrationCode: ""
    })
  }
});
